package presentation;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

/**
 * A beállításokért felelős ablak.
 * @author Szabolcs
 *
 */
@SuppressWarnings("serial")
public class OptionsFrame extends JFrame {
	private MainFrame mf;
	private JLabel title;
	private JLabel player1;
	private JLabel player2;
	private JLabel player3;
	private JLabel player4;
	private JLabel nrLabel;
	private JPanel mainPanel;
	private JPanel centerPanel;
	private JTextField name1;
	private JTextField name2;
	private JTextField name3;
	private JTextField name4;
	private JComboBox<Integer> nrText;
	private JButton save;

	private class SaveActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			save();
		}
	}

	/**
	 * Beállítja a mezőket.
	 * 
	 * @param mf a szülő ablak
	 */
	public OptionsFrame(MainFrame mf) {
		this.mf = mf;

		mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout());

		title = new JLabel("A Játékosok beállításai");
		title.setFont(new Font("Arial", Font.BOLD, 18));
		title.setHorizontalAlignment(SwingConstants.CENTER);
		mainPanel.add(title, BorderLayout.NORTH);

		centerPanel = new JPanel();
		centerPanel.setLayout(new GridLayout(5, 2, 2, 2));

		nrLabel = new JLabel("Játékosok száma:");
		nrText = new JComboBox<Integer>();
		nrText.addItem(4);
		nrText.addItem(3);
		nrText.addItem(2);
		nrText.addItem(1);
		centerPanel.add(nrLabel);
		centerPanel.add(nrText);

		player1 = new JLabel("1. játékos:");
		player2 = new JLabel("2. játékos:");
		player3 = new JLabel("3. játékos:");
		player4 = new JLabel("4. játékos:");

		name1 = new JTextField("John");
		name2 = new JTextField("Pam");
		name3 = new JTextField("Greg");
		name4 = new JTextField("Gil");

		centerPanel.add(player1);
		centerPanel.add(name1);
		centerPanel.add(player2);
		centerPanel.add(name2);
		centerPanel.add(player3);
		centerPanel.add(name3);
		centerPanel.add(player4);
		centerPanel.add(name4);

		mainPanel.add(centerPanel, BorderLayout.CENTER);

		save = new JButton("Mentés");

		mainPanel.add(save, BorderLayout.SOUTH);
		save.addActionListener(new SaveActionListener());
		this.setContentPane(mainPanel);

		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				close();
			}
		});
	}

	/**
	 * Menti az adott beállításokat.
	 */
	private void save() {
		mf.setPlayers((int) nrText.getSelectedItem(), name1.getText(),
				name2.getText(), name3.getText(), name4.getText());
		close();
		this.dispose();
	}

	/**
	 * Bezárja az ablakot.
	 */
	private void close() {
		mf.setEnabled(true);
	}
}
